//
//  MainViewController.swift
//  weibo1
//
//  Created by apple on 15/11/3.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //MARK:监听方法
    //点击撰写按钮
    ///// 如果`单纯`使用 `private` 运行循环将无法正确发送消息，导致崩溃
    /// 如果使用 @objc 修饰符号，可以保证运行循环能够发送此消息，即使函数被标记为 private
  @objc  private func clickComposedButton(){
    
    print("点我了")
    
    
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        addChildViewControllers()
        
        setupCompossedButton()

    }
    override func viewWillAppear(animated: Bool) {
        // 会创建 tabBar 中的所有控制器对应的按钮！
        super.viewWillAppear(animated)
        //jiang撰写按钮弄到最前面
        tabBar.bringSubviewToFront(composedButton)
    }
    //mark:懒加载
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")

   }

    
    
    
    
    //mark:设置界面
    
extension MainViewController {

    ///设置撰写按钮
  func setupCompossedButton(){
    
    //1添加按钮
        tabBar.addSubview(composedButton)
        
    //2调整按钮
    let count = childViewControllers.count
    //让按钮宽一点点
    let w = tabBar.bounds.width / CGFloat(count) - 1
    composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    //3.添加监听方法
    composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    
    }
    
    
    //添加所有控制器
    func  addChildViewControllers(){
        
        //设置tintColor-图片的渲染颜色
        //
        //tabBar.tintColor = UIColor.orangeColor()
    addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    
    }
    
    //添加控制器
    
    /// <#Description#>
    ///
    /// - parameter vc:        vc
    /// - parameter title:     biaoti
    /// - parameter imageName: 图像名称
    func addChildViewController(vc:UIViewController,title:String,imageName:String){
    //设置标题
        vc.title = title
        //设置图像
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    
    
    }
    
}